package com.zhai.test.domain.activity;

import com.zhai.domain.activity.model.entity.PartakeRaffleActivityEntity;
import com.zhai.domain.activity.model.entity.SkuRechargeEntity;
import com.zhai.domain.activity.model.entity.UserRaffleOrderEntity;
import com.zhai.domain.activity.service.IRaffleActivityAccountQuotaService;
import com.zhai.domain.activity.service.IRaffleActivityPartakeService;
import com.zhai.domain.activity.service.armory.IActivityArmory;
import com.zhai.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;

/**
 * @Description:
 * @ClassName: RaffleOrderTest
 * @Author: zhaiyongxin
 * @Date: 2025/2/19 17:51
 * @Version: 1.0
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RaffleOrderTest {

    @Resource
    private IRaffleActivityAccountQuotaService raffleOrder;
    @Resource
    private IActivityArmory activityArmory;
    @Resource
    private IRaffleActivityPartakeService activityPartakeService;

    @Before
    public void setUp(){
        //活动sku装配
        log.info("装配活动：{}", activityArmory.assembleActivitySku(9011L));
    }

    @Test
    public void test_createRaffleActivitySkuOrder() throws InterruptedException {
        SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
        skuRechargeEntity.setUserId("zhaiyongxin");
        skuRechargeEntity.setSku(9011L);
        skuRechargeEntity.setOutBusinessNo(RandomStringUtils.randomNumeric(12));
        String orderId = raffleOrder.createOrder(skuRechargeEntity);
        log.info("测试结果：{}", orderId);
        new CountDownLatch(1).await();
    }

    @Test
    public void test_createSkuRechargeOrder() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            try {
                SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
                skuRechargeEntity.setUserId("zhaiyongxin");
                skuRechargeEntity.setSku(9011L);
                // outBusinessNo 作为幂等仿重使用，同一个业务单号2次使用会抛出索引冲突 Duplicate entry '700091009111' for key 'uq_out_business_no' 确保唯一性。
                skuRechargeEntity.setOutBusinessNo(RandomStringUtils.randomNumeric(12));
                String orderId = raffleOrder.createOrder(skuRechargeEntity);
                log.info("测试结果：{}", orderId);
            } catch (AppException e) {
                log.warn(e.getInfo());
            }
        }

        new CountDownLatch(1).await();
    }

    @Test
    public void test_createRaffleActivityOrder() {
        UserRaffleOrderEntity userRaffleOrder = activityPartakeService.createOrder(PartakeRaffleActivityEntity.builder()
                .activityId(100301L)
                .userId("zhaiyongxin")
                .build());
        log.info("测试结果：{}", userRaffleOrder);
    }

}
